package edu.gdut.tiantian.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

import edu.gdut.tiantian.dao.OrderItemDao;
import edu.gdut.tiantian.entity.CartItem;
import edu.gdut.tiantian.entity.Order;
import edu.gdut.tiantian.entity.User;
import edu.gdut.tiantian.service.Impl.CartService;
import edu.gdut.tiantian.service.Impl.OrderService;
import edu.gdut.tiantian.service.Impl.UserService;
import edu.gdut.tiantian.utils.UUIDUtils;

@Controller
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private OrderItemDao orderItemDao;

	@Autowired
	private CartService cartService;

	@Autowired
	private UserService userService;

	@RequestMapping("/findAll.do")
	public ModelAndView findByCondition(Integer pageNum, HttpServletRequest req) {
		Map<String, String> condition = new HashMap<>();
		Enumeration<String> paramNames = req.getParameterNames();
		StringBuffer conditionStr = new StringBuffer();
		while (paramNames.hasMoreElements()) {
			String key = paramNames.nextElement();
			String value = req.getParameter(key);
			if (value != null && value.trim().length() != 0) {
				condition.put(key, value);
				conditionStr.append("&" + key + "=" + value);
			}
		}
		ModelAndView mv = new ModelAndView();
		List<Order> OrderList = orderService.findByCondition(condition, pageNum);
		PageInfo<Order> pageInfo = new PageInfo<Order>(OrderList);
		mv.addObject("condition", conditionStr);
		mv.addObject("pageInfo", pageInfo);
		mv.setViewName("background/order-manager");
		return mv;
	}

	@RequestMapping("/findById.do")
	public ModelAndView findById(String orderId) {
		ModelAndView mv = new ModelAndView();
		Order order = orderService.findByOrderId(orderId);
		mv.addObject("order", order);
		mv.setViewName("background/order-findByid");
		return mv;
	}

	@RequestMapping("/user-getByOrderId.do")
	public ModelAndView user_findById(String orderId) {
		ModelAndView mv = new ModelAndView();
		Order order = orderService.findByOrderId(orderId);
		mv.addObject("order", order);
		mv.setViewName("user/order-show");
		return mv;
	}

	@RequestMapping("updateStatus.do")
	public String updateStatus(String orderId) {
		orderService.updateStatus(orderId);
		return "redirect:/order/findAll.do?pageNum=0";
	}

	@RequestMapping("/addOrder.do")
	public String addOrder(Order order, String userId, HttpServletRequest req) {
		String orderId = UUIDUtils.getUUId();
		orderService.addOrder(order, userId, orderId);
		@SuppressWarnings("unchecked")
		List<CartItem> cartItems = (List<CartItem>) req.getSession().getAttribute("cartItems");
		System.out.println(cartItems);
		for (CartItem cartItem : cartItems) {
			orderItemDao.addToOrder(cartItem.getProduct().getProductId(), orderId, cartItem.getBuyNum());
		}
		cartService.clearCart(userId);
		return "redirect:/order/getMyOrder.do?userId=" + userId;
	}

	@RequestMapping("/getMyOrder.do")
	public ModelAndView getMyOrder(String userId) {
		ModelAndView mv = new ModelAndView();
		User user = userService.getUser(userId);
		mv.addObject("user", user);
		mv.setViewName("user/myOrder");
		return mv;
	}

	@RequestMapping("/withdraw.do")
	public String withdraw(String orderId, String userId) {
		orderService.withdraw(orderId);
		return "redirect:/order/getMyOrder.do?userId=" + userId;
	}

	@RequestMapping("/user-findOrder.do")
	public ModelAndView user_findOrder(String productName,String userId, HttpServletRequest req) {
		ModelAndView mv = new ModelAndView();
		Map<String,String> condition = new HashMap<>();
		condition.put("productName", productName);
		condition.put("userId", userId);
		List<Order> rtOrders = orderService.findByCondition(condition, 0);
		if(rtOrders==null||rtOrders.size()==0) {
			mv.addObject("noresult","找不到包含该商品的订单!");
		}else {
			mv.addObject("noresult","");
			mv.addObject("rtOrders",rtOrders);
		}
		mv.setViewName("user/order-search");
		return mv;
	}

}
